package global

import (
	"io/ioutil"
	"log"
	"strings"
)

type SSHHost struct {
	Host      string
	Port      int
	Username  string
	Password  string
	CmdFile   string
	Cmds      string
	CmdList   []string
	Key       string
	LinuxMode bool
	//Result    SSHResult
}

func SplitString(str string) []string {
	var strList []string
	if str == "" {
		return []string{}
	}
	if strings.Contains(str, ",") {
		strList = strings.Split(str, ",")
	} else {
		strList = strings.Split(str, ";")
	}
	return strList
}

//读取文件数据
func GetFileData(path string) ([]string, error) {
	res := []string{}
	fileBytes, err := ioutil.ReadFile(path)
	if err != nil {
		log.Println("read file error", err)
		return res, err
	}
	fileStr := string(fileBytes)
	for _, s := range strings.Split(fileStr, "\n") {
		s = strings.TrimSpace(s)
		if s == "" {
			continue
		}
		res = append(res, s)
	}
	return res, nil
}

func GetIpListFromFile(path string) ([]string, error) {
	res, err := GetFileData(path)
	if err != nil {
		return nil, err
	}
	ips := []string{}
	if len(res) > 0 {
		for _, ip := range res {
			ips = append(ips, ip)
		}
	}
	return ips, err
}

func GetIpListFromConfig() {

}
